home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Parser / pgenmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  4.6 KB  |  212 lines  |  [TEXT/CWIE]

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Parser generator main program */
  33.  
  34. /* This expects a filename containing the grammar as argv[1] (UNIX)
  35.    or asks the console for such a file name (THINK C).
  36.    It writes its output on two files in the current directory:
  37.    - "graminit.c" gets the grammar as a bunch of initialized data
  38.    - "graminit.h" gets the grammar's non-terminals as #defines.
  39.    Error messages and status info during the generation process are
  40.    written to stdout, or sometimes to stderr. */
  41.  
  42. /* XXX TO DO:
  43.    - check for duplicate definitions of names (instead of fatal err)
  44. */
  45.  
  46. #include "pgenheaders.h"
  47. #include "grammar.h"
  48. #include "node.h"
  49. #include "parsetok.h"
  50. #include "pgen.h"
  51.  
  52. int debugging;
  53.  
  54. /* Forward */
  55. grammar *getgrammar PROTO((char *filename));
  56. #ifdef THINK_C
  57. int main PROTO((int, char **));
  58. char *askfile PROTO((void));
  59. #endif
  60.  
  61. void
  62. goaway(sts)
  63.     int sts;
  64. {
  65.     exit(sts);
  66. }
  67.  
  68. int
  69. main(argc, argv)
  70.     int argc;
  71.     char **argv;
  72. {
  73.     grammar *g;
  74.     FILE *fp;
  75.     char *filename;
  76.     
  77. #ifdef THINK_C
  78.     filename = askfile();
  79. #else
  80.     if (argc != 2) {
  81.         fprintf(stderr, "usage: %s grammar\n", argv[0]);
  82.         goaway(2);
  83.     }
  84.     filename = argv[1];
  85. #endif
  86.     g = getgrammar(filename);
  87.     fp = fopen("graminit.c", "w");
  88.     if (fp == NULL) {
  89.         perror("graminit.c");
  90.         goaway(1);
  91.     }
  92.     printf("Writing graminit.c ...\n");
  93.     printgrammar(g, fp);
  94.     fclose(fp);
  95.     fp = fopen("graminit.h", "w");
  96.     if (fp == NULL) {
  97.         perror("graminit.h");
  98.         goaway(1);
  99.     }
  100.     printf("Writing graminit.h ...\n");
  101.     printnonterminals(g, fp);
  102.     fclose(fp);
  103.     goaway(0);
  104. }
  105.  
  106. grammar *
  107. getgrammar(filename)
  108.     char *filename;
  109. {
  110.     FILE *fp;
  111.     node *n;
  112.     grammar *g0, *g;
  113.     perrdetail err;
  114.     
  115.     fp = fopen(filename, "r");
  116.     if (fp == NULL) {
  117.         perror(filename);
  118.         goaway(1);
  119.     }
  120.     g0 = meta_grammar();
  121.     n = parsefile(fp, filename, g0, g0->g_start,
  122.               (char *)NULL, (char *)NULL, &err);
  123.     fclose(fp);
  124.     if (n == NULL) {
  125.         fprintf(stderr, "Parsing error %d, line %d.\n",
  126.             err.error, err.lineno);
  127.         if (err.text != NULL) {
  128.             int i;
  129.             fprintf(stderr, "%s", err.text);
  130.             i = strlen(err.text);
  131.             if (i == 0 || err.text[i-1] != '\n')
  132.                 fprintf(stderr, "\n");
  133.             for (i = 0; i < err.offset; i++) {
  134.                 if (err.text[i] == '\t')
  135.                     putc('\t', stderr);
  136.                 else
  137.                     putc(' ', stderr);
  138.             }
  139.             fprintf(stderr, "^\n");
  140.             free(err.text);
  141.         }
  142.         goaway(1);
  143.     }
  144.     g = pgen(n);
  145.     if (g == NULL) {
  146.         printf("Bad grammar.\n");
  147.         goaway(1);
  148.     }
  149.     return g;
  150. }
  151.  
  152. #ifdef THINK_C
  153. char *
  154. askfile()
  155. {
  156.     char buf[256];
  157.     static char name[256];
  158.     printf("Input file name: ");
  159.     if (fgets(buf, sizeof buf, stdin) == NULL) {
  160.         printf("EOF\n");
  161.         goaway(1);
  162.     }
  163.     /* XXX The (unsigned char *) case is needed by THINK C 3.0 */
  164.     if (sscanf(/*(unsigned char *)*/buf, " %s ", name) != 1) {
  165.         printf("No file\n");
  166.         goaway(1);
  167.     }
  168.     return name;
  169. }
  170. #endif
  171.  
  172. void
  173. fatal(msg)
  174.     char *msg;
  175. {
  176.     fprintf(stderr, "pgen: FATAL ERROR: %s\n", msg);
  177.     goaway(1);
  178. }
  179.  
  180. #ifdef macintosh
  181. /* ARGSUSED */
  182. int
  183. guesstabsize(path)
  184.     char *path;
  185. {
  186.     return 4;
  187. }
  188. #endif
  189.  
  190. /* No-nonsense my_readline() for tokenizer.c */
  191.  
  192. char *
  193. my_readline(prompt)
  194.     char *prompt;
  195. {
  196.     int n = 1000;
  197.     char *p = malloc(n);
  198.     char *q;
  199.     if (p == NULL)
  200.         return NULL;
  201.     fprintf(stderr, "%s", prompt);
  202.     q = fgets(p, n, stdin);
  203.     if (q == NULL) {
  204.         *p = '\0';
  205.         return p;
  206.     }
  207.     n = strlen(p);
  208.     if (n > 0 && p[n-1] != '\n')
  209.         p[n-1] = '\n';
  210.     return realloc(p, n+1);
  211. }
  212.